跳到主要内容

标记数据

标记编码步骤

倒入有用的软件包

import numpy as np
from sklearn import preprocessing

定义样本标签

# Sample input labels
input_labels = ['red','black','red','green','black','yellow','white']

创建和训练标签编码器对象

# Creating the label encoder
encoder = preprocessing.LabelEncoder()
encoder.fit(input_labels)

通过编码随机排序列表来检查性能

# encoding a set of labels
test_labels = ['green','red','black']
encoded_values = encoder.transform(test_labels)
print("\nLabels =", test_labels)

编码值列表,即将文字标签转换为数字

Encoded values = [1, 2, 0]

通过解码一组随机数来检查性能

# decoding a set of values
encoded_values = [3,0,4,1]
decoded_list = encoder.inverse_transform(encoded_values)
print("\nEncoded values =", encoded_values)